package cn.itcast.web.task;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.system.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class SendEmailTask {
    @Reference
    private ContractService contractService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private UserService userService;
    public void sendEmail(){

        List<Contract> contractList = contractService.findContractAfterThreeDays(new Date());

        if (contractList!= null && contractList.size()>0){
            for (Contract contract : contractList) {

                HashMap<String,String> map = new HashMap<>();
                String createByUserId = contract.getCreateBy();
                if (createByUserId!=null){
                    User user = userService.findById(createByUserId);

                    System.out.println("user憨憨"+user);
                    map.put("email",user.getEmail());
                    map.put("userName",user.getUserName());
                    map.put("contractNo",contract.getContractNo());
                    map.put("customName",contract.getCustomName());
                    //convertAndSend(“交换机名字”,"路由key名字",发送的消息);实现添加用户，发送邮件欢迎

                    rabbitTemplate.convertAndSend("email-exchange","contract.delivery",map);
                    System.out.println("已发送到mq上");
                }

            }
        }
    }
}
